package com.zyd.blog.business.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.zyd.blog.business.service.BizSmalltalktypeService;
import com.zyd.blog.business.service.BizSmalltalktypeService;

import com.zyd.blog.business.vo.SmalltalktypeVO;
import com.zyd.blog.persistence.beans.BizSmalltalktype;
import com.zyd.blog.persistence.beans.BizSmalltalktype;
import com.zyd.blog.business.service.BizSmalltalktypeService;
import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import com.zyd.blog.persistence.mapper.BizSmalltalktypeMapper;
import com.zyd.blog.persistence.mapper.BizSmalltalktypeMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.Assert;
import org.springframework.util.CollectionUtils;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author yuxiaopeng
 * @since 2018-06-14
 */
@Service
public class BizSmalltalktypeServiceImpl implements BizSmalltalktypeService {

    @Autowired
    private BizSmalltalktypeMapper smalltalktypeMapper;

    /**
     * 分页查询
     *
     * @param vo
     * @return
     */
    public PageInfo<BizSmalltalktype> findPageBreakByCondition(SmalltalktypeVO vo){
        PageHelper.startPage(vo.getPageNumber(), vo.getPageSize());
        List<BizSmalltalktype> list = smalltalktypeMapper.findPageBreakByCondition(vo);

        PageInfo bean = new PageInfo<BizSmalltalktype>(list);
        bean.setList(list);

        return bean;
    }

    /**
     * 保存一个实体，null的属性不会保存，会使用数据库默认值
     *
     * @param entity
     * @return
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public BizSmalltalktype insert(BizSmalltalktype entity) {
        Assert.notNull(entity, "说说类型-不可为空！");
        entity.setUpdateTime(new Date());
        entity.setCreateTime(new Date());
        smalltalktypeMapper.insertSelective(entity);
        return entity;
    }

    /**
     * 批量插入，支持批量插入的数据库可以使用，例如MySQL,H2等，另外该接口限制实体包含id属性并且必须为自增列
     *
     * @param entities
     */
    @Override
    public void insertList(List<BizSmalltalktype> entities) {
        Assert.notNull(entities, "说说类型-不可为空！");
        List<BizSmalltalktype> list = new ArrayList<>();
        for (BizSmalltalktype entity : entities) {
            entity.setUpdateTime(new Date());
            entity.setCreateTime(new Date());
            list.add(entity);
        }
        smalltalktypeMapper.insertList(list);
    }

    /**
     * 根据主键字段进行删除，方法参数必须包含完整的主键属性
     *
     * @param primaryKey
     * @return
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean removeByPrimaryKey(Long primaryKey) {
        return smalltalktypeMapper.deleteByPrimaryKey(primaryKey) > 0;
    }

    /**
     * 根据主键更新实体全部字段，null值会被更新
     *
     * @param entity
     * @return
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean update(BizSmalltalktype entity) {
        Assert.notNull(entity, "说说类型-不可为空！");
        entity.setUpdateTime(new Date());
        return smalltalktypeMapper.updateByPrimaryKey(entity) > 0;
    }

    /**
     * 根据主键更新属性不为null的值
     *
     * @param entity
     * @return
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean updateSelective(BizSmalltalktype entity) {
        Assert.notNull(entity, "说说类型-不可为空！");
        entity.setUpdateTime(new Date());
        return smalltalktypeMapper.updateByPrimaryKeySelective(entity) > 0;
    }

    ///

    /**
     * 根据主键字段进行查询，方法参数必须包含完整的主键属性，查询条件使用等号
     *
     * @param primaryKey
     * @return
     */
    @Override
    public BizSmalltalktype getByPrimaryKey(Long primaryKey) {
        Assert.notNull(primaryKey, "PrimaryKey不可为空！");
        BizSmalltalktype entity = smalltalktypeMapper.selectByPrimaryKey(primaryKey);
        return null == entity ? null : entity;
    }

    /**
     * 根据实体中的属性进行查询，只能有一个返回值，有多个结果时抛出异常，查询条件使用等号
     *
     * @param entity
     * @return
     */
    @Override
    public BizSmalltalktype getOneByEntity(BizSmalltalktype entity) {
        Assert.notNull(entity, "说说类型-不可为空！");
        BizSmalltalktype bo = smalltalktypeMapper.selectOne(entity);
        return null == bo ? null : bo;
    }

    /**
     * 查询全部结果，listByEntity(null)方法能达到同样的效果
     *
     * @return
     */
    @Override
    public List<BizSmalltalktype> listAll() {
        SmalltalktypeVO vo = new SmalltalktypeVO();
        vo.setPageNumber(1);
        vo.setPageSize(100);
        PageHelper.startPage(vo.getPageNumber(), vo.getPageSize());
        List<BizSmalltalktype> entityList = smalltalktypeMapper.findPageBreakByCondition(null);

        return entityList;
    }

    /**
     * 根据实体中的属性值进行查询，查询条件使用等号
     *
     * @param entity
     * @return
     */
    @Override
    public List<BizSmalltalktype> listByEntity(BizSmalltalktype entity) {
        Assert.notNull(entity, "说说类型-不可为空！");
        List<BizSmalltalktype> entityList = smalltalktypeMapper.select(entity);
        if (CollectionUtils.isEmpty(entityList)) {
            return null;
        }
        return entityList;
    }






}
